package com.lhj.jpa.future;

import java.util.concurrent.CompletableFuture;

/**
 * applyToEither
 * 线程1 和 线程2 一起执行，谁先执行完成就用谁的结果返回。
 * @author linghaijin781
 * @date 2021/9/2210:32 下午
 */
public class _02_applyToEither {
	public static void main(String[] args) {
		SmallTool.printTimeAndThread("小白打算坐公交回家");
		SmallTool.printTimeAndThread("797路 和 706路都可以回家");

		CompletableFuture<String> bus = CompletableFuture.supplyAsync(() -> {
			SmallTool.printTimeAndThread("797路公交车即将进站");
			SmallTool.sleepMills(100);
			return "797路公交车进站";
		}).applyToEither(CompletableFuture.supplyAsync(() -> {
			SmallTool.printTimeAndThread("706路公交车即将进站");
			SmallTool.sleepMills(200);
			return "706路公交车进站";
		}), firstComeBus -> firstComeBus);

		SmallTool.printTimeAndThread(String.format("%s, 小白上车回家", bus.join()));
	}
}
